package com.ifast.sys.controller;



import com.ifast.common.base.AdminBaseController;
import com.ifast.common.utils.FileUtil;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-08-01 09:39:15 | com.wondersgroup</small>
 */
@Controller
@RequestMapping("/download")
public class DownLoadController extends AdminBaseController {

	@Value("${file.upload}")
	private String upload ;

	/**
	 * 根据excelname 下载导入文件
	 */
	@RequestMapping("/getExcel")
	public void getExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			String excelPathName = request.getParameter("excelPathName");
			if (StringUtils.isNotBlank(excelPathName)) {
				File file = new File(upload, excelPathName);
				if (file.exists() && file.isFile()) {
					download(request, response, file, excelPathName);
				}else{
					sendJson(response,"未获取到文件!");
					return;
				}
			}else{
				sendJson(response,"未获取到文件!");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}




	/**
	 * 下载帮助类
	 *
	 * @param file
	 *            文件
	 * @param fileName
	 *            文件名
	 *
	 */
	private void download(HttpServletRequest request, HttpServletResponse response, File file, String fileName)
			throws IOException {
		java.io.BufferedInputStream bis = null;
		java.io.BufferedOutputStream bos = null;
		try {
			request.setCharacterEncoding("UTF-8");
			response.setContentType(FileUtil.getMineType(request.getSession().getServletContext(), fileName));
			response.setHeader( "Content-disposition", "attachment; filename=" + setFileNameCode(fileName,request));
			response.setHeader("Content-Length", String.valueOf(file.length()));

			bis = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
			bos = new BufferedOutputStream(response.getOutputStream());

			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
			bos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}

	private String setFileNameCode(String fileName,HttpServletRequest request) throws UnsupportedEncodingException {
		String userAgent = request.getHeader("user-agent");
		if (userAgent != null && userAgent.indexOf("Firefox") >= 0 || userAgent.indexOf("Chrome") >= 0 || userAgent.indexOf("Safari") >= 0) {
			if(userAgent.indexOf("Edge") >= 0){
				fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
			}else{
				fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");}
		} else {
			fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
		}
		return fileName;
	}
	
}
